#pragma once

/**
 * @copyright Copyright (c) 2025 
 * CSG Digital Grid Technology (Guangdong) Co., Ltd.
 * @author zhouyan
 * @brief iec101数据详细定义
*/
#include "xtTypes.h"
#include "IEC_def.h"

namespace XIEC101 {
#pragma  pack (push,1)
    enum {
        SIGNALE_BYTE_FRAME = 0xE5,
        START_CHAR_CONST = 0x10,
        START_CHAR_VARIABLE = 0x68,
        END_CHAR = 0x16,
    };
    union C_CtrlField{
        struct  {
            xuint8 FC : 4;
            xuint8 FCV : 1;
            xuint8 FCB : 1;
            xuint8 PRM : 1;
            xuint8 RES : 1;
        }bits;
        xuint8 data;
        C_CtrlField() : data(0) {bits.PRM = 1;}
    };
    union M_CtrlField {
        struct {
            xuint8 FC : 4;
            xuint8 DFC : 1;
            xuint8 ACD : 1;
            xuint8 PRM : 1;
            xuint8 RES : 1;
        }bits;
        xuint8 data;
        M_CtrlField() : data(0) {}
        M_CtrlField(xuint8 fc, bool acd = 0) : data(0) {
            bits.FC = fc;
            bits.ACD = acd;
        }
    };
    enum C_FuncCode {
        FC_REMOTE_LINK_RESET,
        FC_USER_PROCESS_RESET,
        FC_TEST_LINK_FUNC,
        FC_TRANSFER_DATA_WITH_ACK,
        FC_TRANSFER_DATA_NO_ACK,
        FC_REQUEST_ACCESS = 8,
        FC_REQUEST_LINK_STATUS,
        FC_REQUEST_PRIMARY_DATA,
        FC_REQUEST_SECONDARY_DATA
    };
    enum M_FuncCode {
        FC_ACCEPT_CONFIRM,
        FC_DENY_CONFIRM,
        FC_ACK_WITH_DATA = 8,
        FC_ACK_NO_DATA,
        FC_ACK_WITH_LINK_STATUS = 11,
        FC_LINK_NO_READY = 14,
        FC_LINK_NO_RUN
    };
    inline xuint8 getCheckSum(const xuint8* data, xuint32 len) {
        xuint8 checkSum = 0;
        for(xuint32 i = 0; i < len; i++)
            checkSum += data[i];
        return checkSum;
    }
#pragma pack(pop)
    struct Settings {
        XIEC::Settings iec_settings;
        xuint8 link_addr_len;
    };
};
